package com.volunteer.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.volunteer.entity.User;
import com.volunteer.entity.VolunteerProfile;
import com.volunteer.mapper.UserMapper;
import com.volunteer.mapper.VolunteerProfileMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * 用户服务
 */
@Service
@RequiredArgsConstructor
public class UserService {

    private final UserMapper userMapper;
    private final VolunteerProfileMapper volunteerProfileMapper;
    private final PasswordEncoder passwordEncoder;

    /**
     * 分页查询用户列表
     */
    public IPage<User> getUserList(int page, int size, String keyword, String role) {
        Page<User> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        
        if (keyword != null && !keyword.isEmpty()) {
            queryWrapper.and(wrapper -> wrapper
                    .like(User::getUsername, keyword)
                    .or()
                    .like(User::getNickname, keyword)
                    .or()
                    .like(User::getPhone, keyword));
        }
        
        if (role != null && !role.isEmpty()) {
            queryWrapper.eq(User::getRole, role);
        }
        
        queryWrapper.orderByDesc(User::getCreateTime);
        return userMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 获取用户信息
     */
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    /**
     * 根据用户名获取用户
     */
    public User getUserByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 更新用户信息
     */
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    /**
     * 修改密码
     */
    public void changePassword(Long userId, String oldPassword, String newPassword) {
        User user = userMapper.selectById(userId);
        if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
            throw new RuntimeException("原密码错误");
        }
        user.setPassword(passwordEncoder.encode(newPassword));
        userMapper.updateById(user);
    }

    /**
     * 启用/禁用用户
     */
    public void toggleUserStatus(Long userId) {
        User user = userMapper.selectById(userId);
        user.setStatus(user.getStatus() == 1 ? 0 : 1);
        userMapper.updateById(user);
    }

    /**
     * 获取志愿者信息
     */
    public VolunteerProfile getVolunteerProfile(Long userId) {
        LambdaQueryWrapper<VolunteerProfile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VolunteerProfile::getUserId, userId);
        return volunteerProfileMapper.selectOne(queryWrapper);
    }

    /**
     * 更新志愿者信息
     */
    public void updateVolunteerProfile(VolunteerProfile profile) {
        volunteerProfileMapper.updateById(profile);
    }

    /**
     * 获取志愿者列表
     */
    public IPage<VolunteerProfile> getVolunteerList(int page, int size) {
        Page<VolunteerProfile> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<VolunteerProfile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(VolunteerProfile::getTotalPoints);
        return volunteerProfileMapper.selectPage(pageParam, queryWrapper);
    }
}

